package caidan;

import java.awt.Color;
import java.util.ArrayList;

import javax.swing.JDialog;

import caidan.data.CategoryData;
import caidan.data.FoodData;
import jinyilw.swing.FontTools;
import jinyilw.swing.ScreenTools;
import jinyilw.swing.visuals.Rcomp.RDialog;

public abstract class EditerDialog extends RDialog
{
	private static final long serialVersionUID = -2048923677245744300L;
	private static int section = 2, minLeftB = 16;
	private EditContentPanel editContentPanel;
	ArrayList<FoodData>[] foodDataLists;
	String[] categoryNames;
	int[] chooseNums, categoryCols;
	int[] nums, cols;

	public EditerDialog(JDialog jd, boolean b)
	{
		super(jd, b);
		setOptionStyle(100, 1f);
		setTitleColor(Color.yellow);
		setTitleFont(FontTools.getCustomFont(17));
		setSection(section);
		init();
	}

	void init()
	{
		nums = isBreakfast()
				? MainApp.breakfastChooseNums
				: MainApp.otherChooseNums;
		cols = MainApp.columns;

		int length = 0;
		for (int n : nums)
			if (n > 0)
				length++;
		if (length < nums.length)
		{
			categoryNames = new String[length];
			chooseNums = new int[length];
			categoryCols = new int[length];
			int ii = 0;
			for (int i = 0; i < nums.length; i++)
				if (nums[i] > 0)
				{
					categoryNames[ii] = MainApp.categoryNames[i];
					chooseNums[ii] = nums[i];
					categoryCols[ii] = cols[i];
					ii++;
				}
		} else
		{
			categoryNames = MainApp.categoryNames;
			chooseNums = nums;
			categoryCols = cols;
		}
		foodDataLists = getFoodDataLists(isBreakfast(), length);
		editContentPanel = new EditContentPanel(foodDataLists, categoryCols,
				chooseNums, categoryNames, this);
		setContentPane(editContentPanel);
		setDefaultDimensionRatio(0.92, 0.92);
		defaultSize();
		// setResizable(false);
		setLocationRelativeTo(null);
	}

	ArrayList<FoodData>[] getFoodDataLists(boolean isBreakfast, int length)
	{
		CategoryData[] categoryDatas = MainApp.getCategoryDatas();
		ArrayList<FoodData>[] foodLists = new ArrayList[length];
		if (length == categoryDatas.length)
			for (int i = 0; i < length; i++)
				foodLists[i] = categoryDatas[i].getCurrentDatas(isBreakfast);
		else
		{
			int ii = 0;
			for (int i = 0; i < nums.length; i++)
				if (nums[i] > 0)
				{
					foodLists[ii] = categoryDatas[i]
							.getCurrentDatas(isBreakfast);
					categoryDatas[i].setEditID(isBreakfast, ii);
					ii++;
				} else
					categoryDatas[i].setEditID(isBreakfast, -1);
		}
		for (CategoryData categoryData : categoryDatas)
		{
			categoryData.addDataAddedActor(
					(FoodData foodData, Boolean isBreakfast1) ->
					{
						if (isBreakfast() == isBreakfast1)
							new Thread(() ->
							{
								addNew(categoryData, foodData);
							}).start();
					});
			categoryData.addDataDeletedActor(
					(Integer index, Boolean isBreakfast1) ->
					{
						if (isBreakfast() == isBreakfast1)
							new Thread(() ->
							{
								delete(categoryData, index);
							}).start();
					});
			categoryData.addNameChangedOperater((String name) ->
			{
				setCategoryName(name, categoryData.getEditID(isBreakfast()));
			});
		}
		return foodLists;
	}

	public abstract boolean isBreakfast();

	public void setCurrentMeal(OneMealPanel oneMeal)
	{
		// currentMeal = oneMeal;
		String title = "食谱编辑 — " + MainApp.dayStrings[oneMeal.getDay()] + " "
				+ MainApp.mealStrings[oneMeal.getMeal()];
		setTitle(title);
		editContentPanel.setCurrentMeal(oneMeal);
	}

	public void addNew(CategoryData categoryData, FoodData... foodDatas)
	{

		int editListsID = categoryData.getEditID(isBreakfast());
		editContentPanel.addNew(editListsID, foodDatas);
	}

	public void delete(CategoryData categoryData, int index)
	{
		int editListsID = categoryData.getEditID(isBreakfast());
		editContentPanel.delete(editListsID, index);
	}

	public void setCategoryName(String name, int id)
	{
		editContentPanel.categoryLabels[id].setText(name);
	}

	@Override
	public void setBounds(int x, int y, int width, int height)
	{
		if (editContentPanel == null || editContentPanel.getMaxCol() == 0)
			return;
		if (width == getWidth() && height == getHeight())
		{
			super.setBounds(x, y, width, height);
			return;
		}
		int w = (width - (section + minLeftB) * 2)
				/ editContentPanel.getMaxCol();
		int ss = w / 15;
		int ww = w - ss;
		int hh = ww * 8 / 9;
		editContentPanel.positionConfiger.updatePramater(ww, hh, ss, 0,
				minLeftB + ss / 2);
		height = editContentPanel.getPreferredHeight() + getTitleHeight()
				+ section;
		width = editContentPanel.getPreferredWidth() + section * 2;
		int maxHeight = ScreenTools.getDefaultScreenSize().height - 100;
		if (height > maxHeight)
			height = maxHeight;
		super.setBounds(x, y, width, height);
	}
}
